package com.junxonline.plat.controller.category;

import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.category.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: CategoryController
 * @Description: 收费类别控制器
 */

@RestController
@RequestMapping(value = "/admin/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;
    
    /**
     * 获取收费类别数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getCategory")
    public ResultVO getCategory(@RequestBody QueryCategoryVO vo) {

        if (CommonUtils.isEmpty(vo.getCategoryId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(categoryService.getCategory(vo));
    }

    /**
     * 筛选查询收费类别数据
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryCategory")
    public PageResultVO queryCategory(PageVO page, QueryCategoryVO vo) {
        return getReturnSuccess(categoryService.queryCategory(page, vo));
    }


    /**
     * 添加收费类别
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addCategory")
    public ResultVO addCategory(@RequestBody CategoryVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getCategoryName())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = categoryService.addCategory(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改收费类别
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editCategory")
    public ResultVO editCategory(@RequestBody CategoryVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getCategoryId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = categoryService.editCategory(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除收费类别
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delCategory")
    public ResultVO delCategory(@RequestBody CategoryIdsVO vo) {
        if (CommonUtils.isEmpty(vo)) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] categoryIdsArray = vo.getCategoryIds();

        boolean successMenu = categoryService.delCategory(categoryIdsArray);
        if (!successMenu) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
